<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Customer, suspendDelivery } from '@/api/customer'
import CustomerTable from '@/components/Customer/CustomerTable.vue'

const suspendDeliveryButton = (index: number, customer: Customer) => {
  showSuspendDialog.value = true
  suspendMessage.suspendCustomerId = customer.id
}

const showSuspendDialog = ref(false)

const now = new Date()
const suspendMessage = reactive({
  suspendCustomerId: 0,
  dateArr: [now, now],
  reason: ''
})

const customerTableRef = ref()
const submitSuspendMessage = async () => {
  try {
    await suspendDelivery({ ...suspendMessage })
    await customerTableRef.value.getTableData()
    showSuspendDialog.value = false
    ElMessage.success('设置成功')
  } catch (error) {
    ElMessage.error((error as Error).message)
  }
}

</script>

<template>
<CustomerTable isFilterExpired ref="customerTableRef">
  <template #option="{ scope }">
    <el-button type="primary" size="small" @click="suspendDeliveryButton(scope.$index, scope.row)">停送</el-button>
  </template>
</CustomerTable>
<el-dialog v-model="showSuspendDialog"  width="40%" title="停送">
  <el-form :model="suspendMessage">
    <el-form-item label="停送时间">
          <el-date-picker
            v-model="suspendMessage.dateArr"
            type="daterange"
            range-separator="到"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
    </el-form-item>
        <el-form-item label="停送原因">
          <el-input
            type="textarea"
            v-model="suspendMessage.reason"
            :autosize="{ minRows: 3, maxRows: 6 }"
            placeholder="请输入停送原因"
          />
    </el-form-item>
  </el-form>
  <template #footer>
    <span class="dialog-footer">
      <el-button @click="showSuspendDialog = false">取消</el-button>
      <el-button type="primary" @click="submitSuspendMessage"
        >确认</el-button>
    </span>
  </template>
</el-dialog>
</template>
